<?php

/**
 * @return array
 */
function aia_region_field_info() {
  return array(
    'field_aia_region' => array(
      'label' => t('Ссылка на регион'),
      'description' => t('Region reference'),
      'default_widget' => 'aia_region_select',
      'default_formatter' => 'region_link',
    ),
  );
}


function aia_region_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  foreach ($items as $delta => $item) {

    if (!empty($item['field_aia_region'])) {
      if (! is_numeric($item['field_aia_region'])) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'aia_region_invalid',
          'message' => t('Invalid region'),
        );
      }
    }
  }
}

function field_example_field_is_empty($item, $field) {
  return empty($item['field_aia_region']);
}

function aia_region_field_formatter_info() {
  return array(
    'region_link' => array(
      'label' => t('Link to region'),
      'field types' => array('field_aia_region'),
    ),
  );
}

function aia_region_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  switch ($display['type']) {

    case 'region_link':
      foreach ($items as $delta => $item) {
        $rid = $item['field_aia_region'];
        $region = aia_region_load($rid);

        $element[$delta] = array(
          '#type' => 'link',
          '#title' => $region->title,
          '#href' => $region->alias,
          '#options' => array(),
        );
      }

      break;

  }

  return $element;
}

function aia_region_field_widget_info() {
  return array(
    'aia_region_select' => array(
      'label' => t('Выбор региона из списка'),
      'field types' => array('field_aia_region'),
      'settings' => array(),
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_DEFAULT,
      ),
    ),
//    'aia_region_checkboxes' => array(
//      'label' => t('Выбор региона (флажки)'),
//      'field types' => array('field_aia_region'),
//      'settings' => array(),
//      'behaviors' => array(
//        'multiple values' => FIELD_BEHAVIOR_DEFAULT,
//      ),
//    ),
  );
}

function aia_region_field_widget_info_alter(&$info) {
  $info['options_buttons']['field types'][] = 'field_aia_region';
}

/**
 * @param $item
 * @param $field
 * @return bool
 */
function aia_region_field_is_empty($item, $field) {

  if(empty($item['field_aia_region'])) {
    return TRUE;
  }

  return FALSE;
}

function aia_region_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $options = aia_region_load_for_select();

  $default = isset($items[$delta]['field_aia_region']) ? $items[$delta]['field_aia_region'] : key($options);

  $widget = $element;
  $widget['#delta'] = $delta;

  switch ($instance['widget']['type']) {

    case 'aia_region_select':
      $widget += array(
        '#type' => 'select',
        '#default_value' => $default,
        '#options' => $options,
      );
      break;

//    case 'aia_region_checkboxes':
//      $widget += array(
//        '#type' => 'checkboxes',
//        '#default_value' => $default,
//        '#options' => $options,
//      );
//      break;

  }

  $element['field_aia_region'] = $widget;
  return $element;
}

function aia_region_field_widget_error($element, $error, $form, &$form_state) {
  switch ($error['error']) {
    case 'aia_region_invalid':
      form_error($element, $error['message']);
      break;
  }
}